package roleModule.roleComponent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IAuthorityResourceInfo;
import edu.ccut.saturn.component.IParameterContext;

public class Transaction extends DefaultComponent {
	@SuppressWarnings("unchecked")
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		
		List<IAuthorityResourceInfo> withResource = new ArrayList<IAuthorityResourceInfo>();
		List<IAuthorityResourceInfo> withoutResource = new ArrayList<IAuthorityResourceInfo>();
		
		withResource = (List)parameterContext.getParameterValue(0);
		withoutResource = (List)parameterContext.getParameterValue(1);
		
		List<Map<String, String>> withRes = new ArrayList<Map<String, String>>();
		List<Map<String, String>> withoutRes = new ArrayList<Map<String, String>>();
		
		withRes = infoToMap(withResource);
		withoutRes = infoToMap(withoutResource);
		
		parameterContext.setParameter(2, withRes);
		parameterContext.setParameter(3, withoutRes);
	}
	@SuppressWarnings("unused")
	private List<Map<String, String>> infoToMap(
			List<IAuthorityResourceInfo> info) {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		if(info != null) {
			for (IAuthorityResourceInfo key : info) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", key.getResourceKey());
				map.put("name", key.getName());
				map.put("descn", key.getDescription());
				list.add(map);
			}
		}
		return list;
	}
}